package com.ddf.springdata.jpa;

import com.ddf.springdata.jpa.config.MainConfiguration;
import com.ddf.springdata.jpa.dao.UserRepository;
import com.ddf.springdata.jpa.entity.IdCard;
import com.ddf.springdata.jpa.entity.User;
import com.ddf.springdata.jpa.service.IdCardService;
import com.ddf.springdata.jpa.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.sql.Date;
import java.time.LocalDate;

/**
 * @author DDf on 2018/9/24
 */
public class Application {
    protected static ApplicationContext applicationContext;

    static {
        applicationContext = new AnnotationConfigApplicationContext(MainConfiguration.class);
        UserRepository userRepository = applicationContext.getBean(UserRepository.class);
        User existUser = userRepository.getByUsername("ddf");

        if (null == existUser) {
            UserService userService = applicationContext.getBean(UserService.class);
            IdCardService idCardService = applicationContext.getBean(IdCardService.class);
            User user = new User();
            user.setUsername("ddf");
            user.setPassword("123456");
            LocalDate localDate = LocalDate.of(1992, 4, 29);
            user.setBirthDay(Date.valueOf(localDate));
            userService.save(user);

            IdCard idCard = new IdCard();
            idCard.setName(user.getUsername());
            idCard.setCardNo("341224");
            idCard.setAddress("安徽省蒙城县");
            idCard.setUser(user);
            idCardService.save(idCard);
        }
    }

    public static void main(String[] args) {
        System.out.println("先启动初始化数据再测试。。。。。。。。。");
    }
}
